package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class ClearImgJob {
    @Autowired
//    注入jedisPool对象
    private JedisPool jedisPool;
    public void clearImg(){
        System.out.println("进行清理");
//        根据Redis中保存的两个set集合进行差值计算，获得垃圾图片名称的集合
        Set<String> imgSets = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (imgSets!=null){
            for (String picName:imgSets){
//                删除七牛云服务器中的图片
                AliyunOSSUtil.deleteFile(picName);
//                从Redis集合中删除图片名称
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, picName);
            }

        }

    }
}
